package com.lantosec.hmrp.modules.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lantosec.hmrp.core.util.DateUtils;
import com.lantosec.hmrp.modules.system.entity.SysWorkday;
import com.lantosec.hmrp.modules.system.mapper.SysWorkdayMapper;
import com.lantosec.hmrp.modules.system.service.ISysWorkdayService;
import lombok.extern.log4j.Log4j2;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 系统工作日管理 服务实现类
 * </p>
 *
 * @author AutoCode
 * @since 2020-03-24
 */
@Service
@Primary
@Log4j2
public class SysWorkdayServiceImpl extends ServiceImpl<SysWorkdayMapper, SysWorkday> implements ISysWorkdayService {

    @Override
    public void create(SysWorkday entity) {
        Calendar currCal=Calendar.getInstance();
        int currentYear = currCal.get(Calendar.YEAR);
        //获取当年第一天
        Date yearFirst = DateUtils.getYearFirst(currentYear);
        //获取当年最后一天
        Date yearLast = DateUtils.getYearLast(currentYear);
        //获取时间段中的每一天
        List<Date> dates = DateUtils.findDates(yearFirst, yearLast);
        dates.forEach(it->{
            String date = DateUtils.dateToStr(it, "yyyy-MM-dd");
            String[] split = date.split("-");
            entity.setWorkdayDate(date);
            entity.setWorkdayYaer(split[0]);    //年
            entity.setWorkdayMonth(split[1]);   //月
            entity.setWorkdayDay(split[2]);     //日
            currCal.setTime(it);
            entity.setWorkdayWeek(currCal.get(Calendar.DAY_OF_WEEK));   //周几，1星期天，2星期一，依此类推
            baseMapper.insert(entity);
        });
    }

    @Override
    public String getWorkDay(String currentDay, Integer num) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date today = null;
        try {
            today = simpleDateFormat.parse(currentDay);
            today = getDate(today, num, simpleDateFormat);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return simpleDateFormat.format(today);
    }

    @Override
    public Date getWorkDay(Date currentDay, Integer num) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        try {
            currentDay = getDate(currentDay, num, simpleDateFormat);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return currentDay;
    }

    private Date getDate(Date currentDay, Integer num, SimpleDateFormat simpleDateFormat) {
        Date tomorrow;
        int delay = 1;
        while(delay <= num){
            tomorrow = DateUtils.getTomorrow(currentDay);
            //判断明天是否是周末或者是否是节假日
            SysWorkday sysWorkday = this.getOne(new QueryWrapper<SysWorkday>()
                    .eq(SysWorkday.ColName.workday_date.toString(),simpleDateFormat.format(tomorrow)));
            if(sysWorkday.getWorkdayType() == 0){
                delay++;
                currentDay = tomorrow;
            }else {
                currentDay = tomorrow;
            }
        }
        return currentDay;
    }

    @Override
    public String getAfterWorkDay(String currentDay, Integer num) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date today = null;
        Date tomorrow = null;
        //将当前日期转成Date
        try {
            today = simpleDateFormat.parse(currentDay);
            int delay = 1;
            while(delay <= num){
                tomorrow = DateUtils.getYesterday(today);
                //判断昨天是否是周末或者是否是节假日
                SysWorkday sysWorkday = this.getOne(new QueryWrapper<SysWorkday>().eq(SysWorkday.ColName.workday_date.toString(),simpleDateFormat.format(tomorrow)));
                if(sysWorkday.getWorkdayType() == 0){
                    delay++;
                    today = tomorrow;
                }else {
                    today = tomorrow;
                }
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return simpleDateFormat.format(today);
    }

    @Override
    public Integer getWorkDayOfMonth(String currentDate) {
        String[] strings = currentDate.split("-");
        String year = strings[0];
        String month = strings[1];
        List<SysWorkday> sysWorkdayList = this.list(new QueryWrapper<SysWorkday>().eq(SysWorkday.ColName.workday_yaer.toString(),year)
                                                                                    .eq(SysWorkday.ColName.workday_month.toString(),month)
                                                                                    .eq(SysWorkday.ColName.workday_type.toString(),0));
        Integer workDayNum = sysWorkdayList.size();
        return workDayNum;
    }

    @Override
    public Integer getWorkDayOfYear(String currentDate) {
        List<SysWorkday> sysWorkdayList = this.list(new QueryWrapper<SysWorkday>().eq(SysWorkday.ColName.workday_yaer.toString(),currentDate)
                                                                                    .eq(SysWorkday.ColName.workday_type.toString(),0));
        Integer workDayNum = sysWorkdayList.size();
        return workDayNum;
    }

    @Override
    public Integer getWorkDayNum(String startDate, String endDate) {
        List<SysWorkday> sysWorkdayList = this.list(new QueryWrapper<SysWorkday>().between(SysWorkday.ColName.workday_date.toString(),startDate,endDate)
                                                                                    .eq(SysWorkday.ColName.workday_type.toString(),0));
        Integer workDayNum = sysWorkdayList.size();
        return workDayNum;
    }


}
